//
//  FunctionListMenu.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15-5-4.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "FunctionListMenu.h"

#define     MenuItemHeight         44

#define     MenuItemSeparatorHeight      0.5

//@interface FunctionListItem : NSObject
//@property(nonatomic,strong) NSString *title;
//@property(nonatomic,assign) BOOL badge;
//@property(nonatomic,assign) BOOL selected;
//@end
//
//@implementation FunctionListItem
//@end

@interface FunctionListMenu ()<XUIButtonDelegate>
{
    __weak UIView *_parentView;
    
    UIView *_backGroundContainView;
    
    CGFloat _moveDistance;
    
    NSMutableArray *_itemDatas;
    
    NSMutableArray *_controls;
}

@end

@implementation FunctionListMenu

-(id)initWithParentView:(UIView*)parentView
{
    self = [super initWithFrame:CGRectZero];
    
    _backGroundContainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    _backGroundContainView.backgroundColor = [UIColor colorWithR:67 G:190 B:237 A:1];
    [self addSubview:_backGroundContainView];
    
    _parentView = parentView;
    
    _controls = [NSMutableArray new];
    
    CGFloat top = [XUINavigationController navigationController].navigationBar.bottom;
    self.frame = CGRectMake(0, top, _parentView.width, _parentView.height - top);

    self.hidden = YES;
    
    self.clipsToBounds = YES;
    
    return self;
}

-(void)addMenuItemWithText:(NSString*)text
{
    if(_controls.count > 0)
    {
        UIView *view = _controls[_controls.count - 1];
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, view.bottom, self.width, MenuItemSeparatorHeight)];
        v.backgroundColor = [UIColor colorWithR:111 G:210 B:247 A:1];
        [_backGroundContainView addSubview:v];
    }
    
    XUIButton *btn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCustom delegate:self];
    btn.frame = CGRectMake(0 , _controls.count * (MenuItemHeight + MenuItemSeparatorHeight), self.width, MenuItemHeight);
    btn.backgroundColor = _backGroundContainView.backgroundColor;
    [btn SetTitle:text];
    [btn SetTitleColor:[UIColor whiteColor]];
    [btn SetFont:[UIFont systemFontOfSize:15]];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_functionlist_check"]];
    imageView.tag = 10000;
    imageView.hidden = YES;
    imageView.right = btn.width - 13;
    imageView.centerY = btn.height / 2;
    [btn addSubview:imageView];
    
    [_controls addObject:btn];
    
    [_backGroundContainView addSubview:btn];
    
    _backGroundContainView.frame = CGRectMake(0, 0, self.width, btn.bottom);
}

-(void)setMenuItemCount:(NSInteger)count atIndex:(NSUInteger)index
{
    XUIButton *btn = _controls[index];
    NSRange range = [[btn GetTitle] rangeOfString:@"（"];
    if(range.location != NSNotFound)
       [btn SetTitle:[NSString stringWithFormat:@"%@（%@）",[[btn GetTitle] substringToIndex:range.location],@(count)]];
    else
        [btn SetTitle:[NSString stringWithFormat:@"%@（%@）",[btn GetTitle],@(count)]];
}

-(void)setMenuItemSelected:(BOOL)selected atIndex:(NSUInteger)index
{
    for(NSUInteger i =0 ;i < _controls.count;i++)
    {
        XUIButton *btn = _controls[i];
        [btn setSelected:NO];
        [btn viewWithTag:10000].hidden = YES;
        if(i == index)
        {
           [btn setSelected:YES];
            [btn viewWithTag:10000].hidden = NO;
        }
    }
}

-(NSUInteger)selectedIndex
{
    for(NSUInteger i =0 ;i < _controls.count;i++)
    {
        XUIButton *btn = _controls[i];
        if(btn.selected == YES)
            return i;
    }
    return NSNotFound;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}

-(void)clickedUpInsideOfButton:(XUIButton *)btn
{
    [_delegate functionListMenu:self clickRowAtIndex:[_controls indexOfObject:btn]];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touche = [touches anyObject];
    CGPoint p = [touche locationInView:self];
    if(CGRectContainsPoint(self.bounds, p))
    {
        [_delegate functionListMenu:self touchEmpty:nil];
    }
}

-(void)show
{
    if(self.hidden == NO)
        return;
    
    self.hidden = NO;
    
    if(_moveDistance <= 0.0)
    {
        _moveDistance = ((XUIButton*)_controls[_controls.count-1]).bottom + MenuItemSeparatorHeight;
        
        _backGroundContainView.bottom -= _moveDistance;
    }
    
    [_parentView addSubview:self];
    
    _parentView.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:0.3 animations:^
    {
         self.backgroundColor = [UIColor colorWithR:0 G:0 B:0 A:0.7];
         
         _backGroundContainView.bottom += _moveDistance;
        
    } completion:^(BOOL finished)
    {
        _parentView.userInteractionEnabled = YES;
    }];
}

-(void)dismiss
{
    if(self.hidden == YES)
        return;
    
    _parentView.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:0.3 animations:^
    {
         self.backgroundColor = [UIColor clearColor];
         
         _backGroundContainView.bottom -= _moveDistance;
        
    } completion:^(BOOL finished)
    {
        _parentView.userInteractionEnabled = YES;
        
        [self removeFromSuperview];
        
        self.hidden = YES;
    }];
}

@end
